package com.hnevc.liaoyanli;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;


public class ZhuCe extends AppCompatActivity {
    private ImageView fanhui;
    private EditText name;
    private EditText sjh;
    private EditText mima;
    private EditText cfmima;
    private Button button;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhuce);
        initView();
        initEvent();
    }

    private void initView() {
        name = findViewById(R.id.name);
        sjh = findViewById(R.id.sjh);
        mima = findViewById(R.id.mima);
        cfmima = findViewById(R.id.mima1);
        button = findViewById(R.id.zhuce2);
    }
    private void initData() {
        String url="http://10.2.64.24/api/user/register";
        String telephone= name.getText().toString().trim();
        String password=mima.getText().toString().trim();
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("telephone",telephone);
            jsonObject.put("name","廖艷麗");
            jsonObject.put("password",password);
        }catch (JSONException e){
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Toast.makeText(getApplicationContext(),jsonObject.optString("msg")+"",Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(ZhuCe.this,"网络异常",Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue queue= Volley.newRequestQueue(ZhuCe.this);
        queue.add(jsonObjectRequest);
    }
    private void initEvent() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }


}
